import {defineConfig} from "vite";
import react from "@vitejs/plugin-react";
import * as path from "path";

const getRandomString = () => {
    const time = new Date();
    const year = time.getFullYear();
    const month = String(time.getMonth() + 1).padStart(2, "0");
    const day = time.getDate().toString().padStart(2, "0");
    const hour = time.getHours().toString().padStart(2, "0");
    const minute = time.getMinutes().toString().padStart(2, "0");
    const second = time.getSeconds().toString().padStart(2, "0");
    return `${year}${month}${day}${hour}${minute}${second}`;
};

export default defineConfig({
    build: {
        outDir: "admin",
        minify: "terser",
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true,
            },
        },
        rollupOptions: {
            output: {
                entryFileNames: `assets/[name]-${getRandomString()}.js`,
                // 块文件名
                chunkFileNames: `assets/[name]-[hash]-${getRandomString()}.js`,
                // 资源文件名 css 图片等等
                assetFileNames: `assets/[name]-[hash]-${getRandomString()}.[ext]`,
            },
        },
    },
    plugins: [react()],
    css: {
        preprocessorOptions: {
            less: {
                javascriptEnabled: true,
            },
        },
    },
    resolve: {
        alias: [
            {
                find: "@",
                replacement: path.resolve(__dirname, "src"),
            },
        ],
    },
    server: {
        proxy: {
            "/youquan": {
                target: "http://118.25.141.195:8089",
                changeOrigin: true,
            },
            "/api": {
                target: "http://admin.alinlearn.cn/service/youquan",
                changeOrigin: true,
                rewrite: path => path.replace(/^\/api/, ""),
            },
        },
    },
});
